<template>
  <div class="business-reviewsplitorders-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-space wrap :size="size">
            <!-- <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.cityArea"
                :options="cityOptions"
                :props="citySelect"
                placeholder="选择地区"
                clearable
              />
            </el-form-item>
            <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.industry"
                :options="industryOptions"
                :props="industrySelect"
                placeholder="选择行业"
                clearable
              />
            </el-form-item> -->
            <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.searchProductClass"
                :options="ProductClassOptions"
                :props="ProductClassSelect"
                placeholder="选择业务类型"
                clearable
                style="width: 160px"
              />
            </el-form-item>
            <el-form-item>
              <el-cascader
                v-model="queryForm.searchCompany"
                :options="CompanyOptions"
                :props="companyProps"
                clearable
                placeholder="按签单组织"
                style="width: 160px"
                @change="getDeptment"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.searchKey"
                clearable
                placeholder="按客户名称"
                @input="trimkey"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.pickStaff"
                clearable
                placeholder="按签单人"
                style="width: 100px"
              />
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="queryForm.OrderType"
                placeholder="按合同属性"
                style="width: 150px"
              >
                <el-option
                  v-for="(item, index) in orderTypeOption"
                  :key="index"
                  :label="item.OrderTypeName"
                  :value="item.OrderTypeID"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.orderCode"
                clearable
                placeholder="按合同编号"
                style="width: 130px"
              />
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="queryForm.signDates"
                style="width: 240px"
                type="daterange"
                placeholder="按签单日期"
                start-placeholder="签单日期起"
                end-placeholder="签单日期止"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="queryForm.assignOrderDate"
                style="width: 240px"
                type="daterange"
                placeholder="按审核分单日期"
                start-placeholder="审核分单起"
                end-placeholder="审核分单止"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
            <el-form-item>
              <el-button :icon="Search" type="primary" @click="queryData">
                查询
              </el-button>
            </el-form-item>
          </el-space>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-tabs
      v-model="queryForm.tab"
      type="border-card"
      @tab-click="handleTabClick"
    >
      <el-tab-pane label="新录单未提单" name="wait">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          /> -->
          <el-table-column
            align="center"
            label="合同ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            prop="OrderType"
            label="合同属性"
            width="100"
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
            width="150"
          />
          <!-- <el-table-column
            align="center"
            prop="createTime"
            label="录单时间"
            width="150"
          /> -->
          <el-table-column
            align="center"
            prop="SignOrderDate"
            label="签单日期"
            width="110"
          />
          <el-table-column
            align="left"
            prop="CustomerAllName"
            label="客户名称"
          />
          <el-table-column
            align="center"
            label="合同业务名称"
            prop="BusinessOrderName"
            show-overflow-tooltip
            width="170"
          />
          <!-- <el-table-column
            align="center"
            prop="SignOrderDeptment"
            label="签单部门"
            width="200"
          /> -->
          <el-table-column
            align="center"
            label="所属组织"
            prop="CompanyName"
            show-overflow-tooltip
            width="100"
          />
          <el-table-column
            align="center"
            prop="SaleStaffName"
            label="签单人"
            width="100"
          />
          <el-table-column align="center" label="业绩核算" width="160">
            <template #default="{ row }">
              <div v-if="row.querenYeji">
                <span v-if="row.querenYeji == 1">核算业绩</span>
                <span v-else>不核算业绩</span>
                <br />
                {{ row.QuerenYejiStaffName }}
                <br />
                {{ row.QuerenYejiTime }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="160">
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="revieworder(row)">
                仅查看不审核分单
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="新提单待审核分单" name="new">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          /> -->
          <el-table-column
            align="center"
            label="合同ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            prop="OrderType"
            label="合同属性"
            width="100"
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
            width="150"
          />
          <!-- <el-table-column
            align="center"
            prop="createTime"
            label="录单时间"
            width="150"
          /> -->
          <el-table-column
            align="center"
            prop="SignOrderDate"
            label="签单日期"
            width="110"
          />
          <el-table-column align="left" label="客户名称">
            <template #default="{ row }">
              <div style="cursor: pointer" @click="getCustomerInfo(row)">
                {{ row.CustomerAllName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="合同业务名称"
            prop="BusinessOrderName"
            show-overflow-tooltip
            width="170"
          />
          <!-- <el-table-column
            align="center"
            prop="SignOrderDeptment"
            label="签单部门"
            width="200"
          /> -->
          <el-table-column
            align="center"
            label="所属组织"
            prop="CompanyName"
            show-overflow-tooltip
            width="100"
          />
          <el-table-column
            align="center"
            prop="SaleStaffName"
            label="签单人"
            width="100"
          />
          <el-table-column align="left" label="查看流程记录" width="120">
            <template #default="{ row }">
              <el-button
                type="primary"
                size="small"
                @click="showflowBtn(row.OrderFlow)"
              >
                查看流程记录
              </el-button>
              <!-- <div
                v-for="(item, index) in row.OrderFlow"
                :key="index"
                style="font-size: 0.75rem"
              >
                {{ item.addTime }}：{{ item.FlowStaffName }}：{{
                  item.OrderFlowName
                }}
              </div> -->
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="160">
            <template #default="{ row }">
              <el-button
                v-if="row.BusinessClassID == 8"
                type="primary"
                size="small"
                @click="revieworder(row)"
              >
                仅审核不分单
              </el-button>
              <el-button
                v-else
                type="primary"
                size="small"
                @click="revieworder(row)"
              >
                审核并分单
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="业绩核算" width="150">
            <template #default="{ row }">
              <div v-if="row.querenYeji">
                <span v-if="row.querenYeji == 1">核算业绩</span>
                <span v-else>不核算业绩</span>
                <br />
                {{ row.QuerenYejiStaffName }}
                <br />
                {{ row.QuerenYejiTime }}
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="已审核并分单" name="reviewed">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          /> -->
          <el-table-column
            align="center"
            label="合同ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            prop="OrderType"
            label="合同属性"
            width="100"
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
            width="150"
          />
          <!-- <el-table-column
            align="center"
            prop="createTime"
            label="录单时间"
            width="150"
          /> -->
          <el-table-column
            align="center"
            prop="SignOrderDate"
            label="签单日期"
            width="110"
          />
          <el-table-column align="left" label="客户名称">
            <template #default="{ row }">
              <div style="cursor: pointer" @click="getCustomerInfo(row)">
                {{ row.CustomerAllName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="合同业务名称"
            prop="BusinessOrderName"
            show-overflow-tooltip
            width="170"
          />
          <!-- <el-table-column
            align="center"
            prop="SignOrderDeptment"
            label="签单部门"
            width="200"
          /> -->
          <el-table-column
            align="center"
            label="所属组织"
            prop="CompanyName"
            show-overflow-tooltip
            width="100"
          />
          <el-table-column
            align="center"
            prop="SaleStaffName"
            label="签单人"
            width="100"
          />
          <el-table-column align="left" label="查看流程记录" width="120">
            <template #default="{ row }">
              <el-button
                type="primary"
                size="small"
                @click="showflowBtn(row.OrderFlow)"
              >
                查看流程记录
              </el-button>
              <!-- <div
                v-for="(item, index) in row.OrderFlow"
                :key="index"
                style="font-size: 0.75rem"
              >
                {{ item.addTime }}：{{ item.FlowStaffName }}：{{
                  item.OrderFlowName
                }}
              </div> -->
            </template>
          </el-table-column>
          <el-table-column align="center" label="审核分单时间" width="130">
            <template #default="{ row }">
              <div style="font-size: 10px; line-height: 20px">
                {{ row.reviewTime }}
                <br />
                {{ row.ReviewStaff }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="130">
            <template #default="{ row }">
              <el-button
                v-if="row.BusinessClassID == 8"
                type="primary"
                size="small"
                @click="revieworder(row)"
              >
                仅审核不分单
              </el-button>
              <el-button
                v-else
                type="primary"
                size="small"
                @click="revieworder(row)"
              >
                已审核并分单
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="业绩核算" width="120">
            <template #default="{ row }">
              <div v-if="row.querenYeji">
                <span v-if="row.querenYeji == 1">核算业绩</span>
                <span v-else>不核算业绩</span>
                <br />
                {{ row.QuerenYejiStaffName }}
                <br />
                {{ row.QuerenYejiTime }}
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane
        v-if="actstaffpostid == 2 || actstaffpostid == 9"
        label="已审核不分单"
        name="onlyreview"
      >
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          /> -->
          <el-table-column
            align="center"
            label="合同ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            prop="OrderType"
            label="合同属性"
            width="100"
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
            width="150"
          />
          <el-table-column
            align="center"
            prop="SignOrderDate"
            label="签单日期"
            width="110"
          />
          <el-table-column
            align="left"
            prop="CustomerAllName"
            label="客户名称"
          />
          <el-table-column
            align="center"
            label="合同业务名称"
            prop="BusinessOrderName"
            show-overflow-tooltip
            width="170"
          />
          <!-- <el-table-column
            align="center"
            prop="SignOrderDeptment"
            label="签单部门"
            width="200"
          /> -->
          <el-table-column
            align="center"
            label="所属组织"
            prop="CompanyName"
            show-overflow-tooltip
            width="100"
          />
          <el-table-column
            align="center"
            prop="SaleStaffName"
            label="签单人"
            width="100"
          />
          <el-table-column align="left" label="查看流程记录" width="120">
            <template #default="{ row }">
              <el-button
                type="primary"
                size="small"
                @click="showflowBtn(row.OrderFlow)"
              >
                查看流程记录
              </el-button>
              <!-- <div
                v-for="(item, index) in row.OrderFlow"
                :key="index"
                style="font-size: 0.75rem"
              >
                {{ item.addTime }}：{{ item.FlowStaffName }}：{{
                  item.OrderFlowName
                }}
              </div> -->
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="160">
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="revieworder(row)">
                查看详情
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="业绩核算" width="150">
            <template #default="{ row }">
              <div v-if="row.querenYeji">
                <span v-if="row.querenYeji == 1">核算业绩</span>
                <span v-else>不核算业绩</span>
                <br />
                {{ row.QuerenYejiStaffName }}
                <br />
                {{ row.QuerenYejiTime }}
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="所有已复核分单" name="allAssignOrder">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          /> -->
          <el-table-column
            align="center"
            label="合同ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            prop="OrderType"
            label="合同属性"
            width="100"
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
            width="150"
          />
          <!-- <el-table-column
            align="center"
            prop="createTime"
            label="录单时间"
            width="150"
          /> -->
          <el-table-column
            align="center"
            prop="SignOrderDate"
            label="签单日期"
            width="110"
          />
          <el-table-column align="left" label="客户名称">
            <template #default="{ row }">
              <div style="cursor: pointer" @click="getCustomerInfo(row)">
                {{ row.CustomerAllName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="合同业务名称"
            prop="BusinessOrderName"
            show-overflow-tooltip
            width="170"
          />
          <!-- <el-table-column
            align="center"
            prop="SignOrderDeptment"
            label="签单部门"
            width="200"
          /> -->
          <el-table-column
            align="center"
            label="所属组织"
            prop="CompanyName"
            show-overflow-tooltip
            width="100"
          />
          <el-table-column
            align="center"
            prop="SaleStaffName"
            label="签单人"
            width="100"
          />
          <el-table-column align="left" label="查看流程记录" width="120">
            <template #default="{ row }">
              <el-button
                type="primary"
                size="small"
                @click="showflowBtn(row.OrderFlow)"
              >
                查看流程记录
              </el-button>
              <!-- <div
                v-for="(item, index) in row.OrderFlow"
                :key="index"
                style="font-size: 0.75rem"
              >
                {{ item.addTime }}：{{ item.FlowStaffName }}：{{
                  item.OrderFlowName
                }}
              </div> -->
            </template>
          </el-table-column>
          <el-table-column align="center" label="审核分单时间" width="130">
            <template #default="{ row }">
              <div style="font-size: 10px; line-height: 20px">
                {{ row.reviewTime }}
                <br />
                {{ row.ReviewStaff }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="130">
            <template #default="{ row }">
              <el-button
                v-if="row.BusinessClassID == 8"
                type="primary"
                size="small"
                @click="revieworder(row)"
              >
                仅审核不分单
              </el-button>
              <el-button
                v-else
                type="primary"
                size="small"
                @click="revieworder(row)"
              >
                已审核并分单
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="业绩核算" width="120">
            <template #default="{ row }">
              <div v-if="row.querenYeji">
                <span v-if="row.querenYeji == 1">核算业绩</span>
                <span v-else>不核算业绩</span>
                <br />
                {{ row.QuerenYejiStaffName }}
                <br />
                {{ row.QuerenYejiTime }}
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
    </el-tabs>
    <!-- <edit ref="editRef" @fetch-data="fetchData" /> -->
    <show-flow ref="showflowRef" />
    <customer-allInfo ref="CustomerAllInfoRef" />
  </div>
</template>

<script>
  import { getCompanySelect, BusinessClassSelect } from '@/api/common' // CitySelect, IndustrySelect,
  import { getBusinessList } from '@/api/businessOrder'
  import CustomerAllInfo from '@/views/components/CustomerAllInfo.vue'
  // import Edit from './components/BusinessReviewsplitordersEdit'
  import ShowFlow from '@/views/business/components/ShowFlow'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'ReviewSplitOrders',
    components: { ShowFlow, CustomerAllInfo },
    setup() {
      // const route = useRoute()
      const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        cityOptions: [],
        industryOptions: [],
        CompanyOptions: [],
        orderTypeOption: [],
        ProductClassOptions: [],
        ProductClassSelect: { expandTrigger: 'hover' },
        companyProps: {
          multiple: true,
          checkStrictly: true,
          expandTrigger: 'hover',
        },
        citySelect: { checkStrictly: true, multiple: true },
        industrySelect: { checkStrictly: true, multiple: true },
        editRef: null,
        showflowRef: null,
        CustomerAllInfoRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          actStffPostID: 0,
          dataType: 'reviewsplitorders',
          tab: 'new',
          pageNo: 1,
          pageSize: 10,
          cityArea: [],
          industry: [],
          searchProductClass: [],
          searchCompany: [],
          orderCode: '',
          pickStaff: '',
          searchKey: '',
          searchOrderFlow: 5,
          orderDate: '',
          OrderType: '',
        },
        activeName: 'new',
        actstaffpostid: 0,
      })
      const trimkey = (val) => {
        state.queryForm.searchKey = val.replace(
          /[^\u4e00-\u9fa5a-zA-Z0-9]/g,
          ''
        )
      }
      const handleTabClick = (tab) => {
        console.log(tab.props.name)
        state.queryForm.tab = tab.props.name
        fetchData()
      }

      const showflowBtn = (flowList) => {
        state.showflowRef.showList(flowList)
      }
      const getCustomerInfo = (row) => {
        state.CustomerAllInfoRef.showEdit(row)
        // router.push({
        //   path: '/customer/Info',
        //   query: { customerid: row.CustomerID },
        // })
      }
      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const revieworder = (row) => {
        const userInfo = JSON.parse(localStorage.getItem('userInfo'))
        const actstaffpost = JSON.parse(userInfo.StaffPostID)
        const actstaffpostid = actstaffpost[0][1]
        router.push({
          path: 'reviewOrder',
          query: { actstaffpostid: actstaffpostid, orderid: row.OrderID },
        })
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        const userInfo = JSON.parse(localStorage.getItem('userInfo'))
        const actstaffpost = JSON.parse(userInfo.StaffPostID)
        state.actstaffpostid = actstaffpost[0][1]
        state.listLoading = true
        const {
          data: { CompanyOptions },
        } = await getCompanySelect()
        const {
          data: { BusinessClassOptions, OrderTypeOption },
        } = await BusinessClassSelect()
        const {
          data: { list, total },
        } = await getBusinessList(state.queryForm)
        state.CompanyOptions = CompanyOptions
        state.ProductClassOptions = BusinessClassOptions
        state.orderTypeOption = OrderTypeOption
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
        // getCitySelect()
        // getIndustrySelect()
      })
      // const getCitySelect = async () => {
      //   const {
      //     data: { cityOptions },
      //   } = await CitySelect()
      //   state.cityOptions = cityOptions
      //   console.log('列表1', cityOptions)
      // }
      // const getIndustrySelect = async () => {
      //   const {
      //     data: { industryOptions },
      //   } = await IndustrySelect()
      //   state.industryOptions = industryOptions
      //   console.log('列表2', industryOptions)
      // }

      return {
        ...toRefs(state),
        handleTabClick,
        setSelectRows,
        revieworder,
        handleDelete,
        handleSizeChange,
        // getCitySelect,
        // getIndustrySelect,
        showflowBtn,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
        trimkey,
        getCustomerInfo,
      }
    },
  })
</script>
